﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.BLL;
using System.Resources;
using System.Threading;
using System.Globalization;
using System.Text;
using EHTWebsiteProject.Code.Entity;

namespace EHTWebsiteProject.Administrator
{
    public partial class EHTLienKetWebSIte : System.Web.UI.Page
    {
        public Boolean IsVN = true;
        private String languageCode = "vi-VN";
        private String message = "";
        private Int32 ID = HangSo.DEFAULT_ID;
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                try
                {
                    languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                    Session["NgonNgu"] = languageCode;
                    if (languageCode == "en-us")
                    {
                        IsVN = false;
                    }

                }
                catch (Exception ex)
                {
                }
                try
                {
                    message = Request.QueryString["Message"].ToString();
                    if (message != "")
                        ltrErorr.Text = HelpMethod.ApplySuccess(message);
                }
                catch
                {

                }
                try
                {
                    ID = Int32.Parse(Request.QueryString["ID"].ToString());

                }
                catch
                {

                }


                if (ID != -1)
                {
                    ViewState["action"] = HangSo.ACTION_UPDATE;
                    ViewState["ID"] = ID;
                    LienKetWebSite theLienKet = LienKetWebSiteProvider.GetLienKetByID(ID);
                    txtMota.Text = theLienKet.MotaVN.ToString();
                    txtMoTaOther.Text = theLienKet.MotaOther.ToString();
                    txtLienKet.Text = theLienKet.Website.ToString();
                }
                else
                    ViewState["action"] = HangSo.ACTION_ADD;
                Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                LoadLanguage(Thread.CurrentThread.CurrentCulture);
                LoadLienKet();
                if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
                    plThongtinVideo.GroupingText = "Thông tin liên kết website";
                else
                    plThongtinVideo.GroupingText = "Link website Infor";
            }            
        }


        /// <summary>
        /// Ham load ngon ngu
        /// </summary>
        /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {
            lbDel.Text = rm.GetString("lbDel", ci);

            lbHelp.Text = rm.GetString("lbHelp", ci);
            lbLinkManager.Text = rm.GetString("lbLinkManager", ci);
            lbMota.Text = rm.GetString("lbMota", ci);
            lbMotaOther.Text = rm.GetString("lbMotaOther", ci);
            ltrLienKetWeb.Text = rm.GetString("ltrLienKetWeb", ci);
        }

        public void LoadLienKet()
        {
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                rpLienKetWebsite.DataSource = LienKetWebSiteProvider.GetLienKet();
                rpLienKetWebsite.DataBind();
            }
            else
            {
                rpLienKetOther.DataSource = LienKetWebSiteProvider.GetLienKet();
                rpLienKetOther.DataBind();
            }
        }

        /// <summary>
        /// ham check all and Uncheck all
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void CheckAll(object sender, System.EventArgs e)
        {
            CheckBox ckAll = (CheckBox)sender;
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                if (ckAll.Checked)
                {
                    foreach (RepeaterItem rp in rpLienKetWebsite.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = true;
                    }
                }
                else
                {
                    foreach (RepeaterItem rp in rpLienKetWebsite.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = false;
                    }
                }
            }
            else
            {
                if (ckAll.Checked)
                {
                    foreach (RepeaterItem rp in rpLienKetOther.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = true;
                    }
                }
                else
                {
                    foreach (RepeaterItem rp in rpLienKetOther.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = false;
                    }
                }
            }
        }


        public void Clear()
        {
            txtMota.Text = "";
            txtMoTaOther.Text = "";
            txtLienKet.Text = "";

        }

        protected void ImgDel_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList arr = new ArrayList();
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (RepeaterItem rp in rpLienKetWebsite.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }
            else
            {
                foreach (RepeaterItem rp in rpLienKetOther.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }

            for (int i = 0; i < arr.Count; i++)
                BanDoProvider.DeleteBanDoByID(Int32.Parse(arr[i].ToString()));
            LoadLienKet();
            ltrErorr.Text = "";
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            String action = ViewState["action"].ToString();

            LienKetWebSite theLienKet = new LienKetWebSite();
            theLienKet.MotaVN = txtMota.Text;
            theLienKet.MotaOther = txtMoTaOther.Text;
            theLienKet.Website = txtLienKet.Text;
            theLienKet.Publish = true;
            theLienKet.NumberOfOrder = 1;

            if (action == HangSo.ACTION_ADD)
            {
                theLienKet.ID = -1;
                LienKetWebSiteProvider.IULienKetWebSite(theLienKet, true);
            }
            else
            {
                theLienKet.ID = Int32.Parse(ViewState["ID"].ToString());
                LienKetWebSiteProvider.IULienKetWebSite(theLienKet, false);
            }
            Clear();
            Response.Redirect("EHTLienKetWebSIte.aspx");
        }
    }
}